const app = getApp()
let qCloud = require("../../../index.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        enterpriseLogo: null,
        enterpriseLogoUrl: null,
        organName: null,
        enterpriseId: null,
        legalName: null,
        enterprisePhone: null,
        enterpriseEmail: null,
        officialWebsite: null,
        businessAddress: null,
        showModalStatus: false,
        organList: [],
    },

    onLoad: function (options) {
        let self = this;
        let userInfo = qCloud.session.getUser();
        self.setData({
            organName: userInfo.organName,
        });
    },

    // 查找申请加入企业
    searchEnterprise() {
        let params = {}
        this.getOrganListAll(params);

        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });

        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
            showModalStatus: true
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export()
            });
        }.bind(this), 200);
    },

    hideEnterpriseModal: function (e) {
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export(),
                showModalStatus: false
            })
        }.bind(this), 200);
        this.setData({
            organList: []
        })
    },

    getOrganListAll(params) {
        let self = this;
        // 发送验证码
        app.fetchApis(self, qCloud.api.getOrganListAll_1, params, "GET", function (e) {
            if (e.data.code == 200) {
                self.setData({
                    organList: e.data.data
                })
            }
        })
    },

    searchInput: function (e) {
        let self = this;
        let params = {
            deptName: e.detail.value
        };
        this.getOrganListAll(params);
    },


    // 确认选择企业
    confirmEnterprise(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            enterpriseName: this.data.organList[index].deptName,
            enterpriseId: this.data.organList[index].deptId,
            legalName: this.data.organList[index].legalName || '',
            enterprisePhone: this.data.organList[index].enterprisePhone || '',
            enterpriseEmail: this.data.organList[index].enterpriseEmail || '',
            officialWebsite: this.data.organList[index].officialWebsite || '',
            businessAddress: this.data.organList[index].businessAddress || '',
            showModalStatus: false
        })
    },

    joinEnterprise: function () {
        let self = this;
        let userInfo = qCloud.session.getUser();
        let params = {
            organId: self.data.enterpriseId,
            organName: self.data.enterpriseName,
            realName: qCloud.crypto.decrypt(userInfo.realNameEncrypt),
            phone: qCloud.crypto.decrypt(userInfo.phoneEncrypt),
            userName: qCloud.crypto.decrypt(userInfo.phoneEncrypt)
        }

        wx.showModal({
            title: '提示',
            content: '确定要加入该企业吗？',
            complete: (res) => {
                if (res.confirm) {
                    app.fetchApis(self, qCloud.api.applyForAdmissionDept, params, "POST", function (e) {
                        if (e.data.code == 200) {
                            wx.showModal({
                                title: '提示',
                                content: "加入成功！请耐心等待企业审核",
                                complete: (res) => {
                                    qCloud.session.clear();
                                    wx.reLaunch({
                                        url: '/pages/login/index',
                                    });
                                }
                            })
                        } else {
                            wx.showToast({
                                title: e.data.msg,
                                icon: "error"
                            })
                        }
                    });
                }
            }
        })


    }

})
